const Clutter    = imports.gi.Clutter;
const GdkPixbuf  = imports.gi.GdkPixbuf;
const GLib       = imports.gi.GLib;
const Lang       = imports.lang;
const Mx         = imports.gi.Mx;
const Octopus    = imports.gi.Octopus;
const OctopusMx  = imports.gi.OctopusMx;

const LoadScreen    = imports.loadScreen;
const MedialibPanel = imports.medialibPanel;
const PlaylistPanel = imports.playlistPanel;
const BottomPanel   = imports.bottomPanel;

function start() {
	//Clutter.init (null);
	this.app = OctopusMx.utils_new_application ("org.octopus.Simple", 0);
	this.app.connect ('startup', Lang.bind(this, function () {
		window.global = Octopus.Global.get ();
		this.config = new Octopus.Configure ();
		let window_width = 1024; //min. width
		let window_height = 768; //min. height
		if (this.config.has_key ("simple", "width")) {
			window_width =  this.config.get_integer("simple", "width");
		}
		if (this.config.has_key("simple", "height")) {
			window_height = this.config.get_integer("simple", "height");
		}
		this.win = app.create_window ("Octopus - Simple");
		this.win.set_has_toolbar (false);
		if (window_width >= 1024 && window_height >= 768) {
			this.win.set_window_size (window_width, window_height);
		} else {
			this.win.set_window_size (1024, 768);
		}
		this.stage = win.get_clutter_stage ();
		this.stage.connect ('destroy', Lang.bind(this, function () {
			this.config.set_integer("simple", "width", this.stage.width);
			this.config.set_integer("simple", "height", this.stage.height);
			this.config.save ();
			return 0;
		}));
		try {
			let iconfile = GLib.build_filenamev ([global.data_path, "octopus-48.png"]);
			let pixbuf = GdkPixbuf.Pixbuf.new_from_file (iconfile);
			OctopusMx.utils_window_set_icon (this.win, pixbuf);
		} catch (e) {
			print ("ERROR loading application icon: " + e.message);
		}

		OctopusMx.MxTracklist.set_stage (this.stage);
		this.stage.connect ('key-press-event', Lang.bind(this, function (/*Clutter.Actor*/ stage, /*Clutter.KeyEvent*/ event) {
			//Ctrl + f
			if (   (event.get_key_symbol () == Clutter.f)
				&& ((event.get_state () & Clutter.ModifierType.CONTROL_MASK) == Clutter.ModifierType.CONTROL_MASK)) {
				this.stage.set_fullscreen (!this.stage.get_fullscreen ());
			}
			//F5 - Refresh
			else if (event.get_key_symbol () == Clutter.KEY_F5) {
				if (this.medialibPanel.tracklistPanelFrame.get_n_children () > 0) {
					this.medialibPanel.tracklistPanel.tracklistPanelClose ();
				}
				this.medialibPanel.load ();
			}
			//Escape
			else if (event.get_key_symbol () == Clutter.KEY_Escape) {
				if (this.medialibPanel.tracklistPanelFrame.get_n_children () > 0) {
					this.medialibPanel.tracklistPanel.tracklistPanelClose ();
				} else if (this.medialibPanel.browsePanel.browsePanelSearchStack.visible) {
					this.medialibPanel.browsePanel.browsePanelSearchEntryClose ();
				} else if (this.stage.get_fullscreen ()) {
					this.stage.set_fullscreen (false);
				}
			} else {
				this.keyFocusActor.onKeyPressEvent (event);
			}
		}));
		this.stage.connect ('notify::allocation', Lang.bind(this, function () {
			this.stageStack.set_width (this.stage.width);
			this.stageStack.set_height (this.stage.height);
		}));

		try {
			//requires mx-css file at index 0
			Mx.Style.get_default ().load_from_file (global.css_files_get (0));
		} catch (e) {
			print ("ERROR loading css file: " + e.message);
		}

		this.stageStack = new Mx.Stack ({ name: 'stage-background' });
		this.stage.add_child (this.stageStack);

		this.medialibPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                          y_align: Clutter.ActorAlign.FILL });
		this.stageStack.add_child (this.medialibPanelFrame);
		this.medialibPanel = new MedialibPanel.MedialibPanel (this.medialibPanelFrame);
		this.keyFocusActor = this.medialibPanel.keyFocusActor;

		this.bottomPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                        y_align: Clutter.ActorAlign.END });
		this.stageStack.add_child (this.bottomPanelFrame);
		this.bottomPanel = new BottomPanel.BottomPanel (this.bottomPanelFrame);

		this.playlistPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.END,
		                                          y_align: Clutter.ActorAlign.FILL });
		this.stageStack.add_child (this.playlistPanelFrame);
		this.playlistPanel = new PlaylistPanel.PlaylistPanel (this.playlistPanelFrame);

		this.loadScreenFrame = new Mx.Frame ();
		this.stageStack.add_child (this.loadScreenFrame);
		this.loadScreen = new LoadScreen.LoadScreen(this.loadScreenFrame);

		this.stageOverlay = new Mx.Frame ({ name: 'stage-overlay' });
		this.stageStack.add_child (this.stageOverlay);

		this.medialibPanel.connect('load-finished', Lang.bind(this, function () {
			this.loadScreenFrame.visible = false;
		}));

		this.medialibPanel.load ();

		//connect to signals
		global.connect('message', Lang.bind(this, function (/*Octopus.Global*/obj, msg) {
			this.bottomPanel.setText (msg);
		}));
		this.medialibPanel.connect('show-message-bar-text', Lang.bind(this, function (medialibPanel, msg) {
			//print ("show-message-bar-text: " + msg);
			this.bottomPanel.setText (msg);
		}));
		this.playlistPanel.connect('show-message-bar-text', Lang.bind(this, function (playlistPanel, msg) {
			this.bottomPanel.setText (msg);
		}));
		this.bottomPanel.connect ('refresh-album-list', Lang.bind(this, function () {
			if (this.medialibPanel.tracklistPanelFrame.get_n_children () > 0) {
				this.medialibPanel.tracklistPanel.tracklistPanelClose ();
			}
			this.medialibPanel.load ();
		}));
		this.bottomPanel.connect ('toggle-fullscreen', Lang.bind(this, function () {
			this.stage.set_fullscreen (!this.stage.get_fullscreen ());
		}));
		this.bottomPanel.connect ('quit', Lang.bind(this, function () {
			this.stage.destroy ();
		}));
		this.playlistPanel.connect('grab-key-focus', Lang.bind(this, function () {
			this.keyFocusActor = this.playlistPanel;
		}));
		this.medialibPanel.connect('grab-key-focus', Lang.bind(this, function () {
			this.keyFocusActor = this.medialibPanel.keyFocusActor;
		}));

		this.stage.show();
	}));

	this.app.run (null);
};
